package com.linkoog.devtools.http.parser.spring.handler;

import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.linkoog.devtools.http.parser.spring.common.SpringConstants;
import com.linkoog.devtools.utils.PsiClassUtils;
import com.linkoog.devtools.utils.PsiElementUtil;
import com.linkoog.devtools.utils.PsiMethodUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SpringHttpUrlHandler {

    public List<String> parseHttpUrl(@NotNull PsiMethod psiMethod){
        PsiClass psiClass = psiMethod.getContainingClass();

        PsiAnnotation classUrlPsiAnnotation = PsiClassUtils.parseClassAnnotation(psiClass, Arrays.asList(SpringConstants.REQUEST_MAPPING), true);
        PsiAnnotation methodUrlPsiAnnotation = PsiMethodUtil.parseMethodAnnotation(psiMethod, Arrays.asList(SpringConstants.REQUEST_MAPPING, SpringConstants.GET_MAPPING,SpringConstants.POST_MAPPING,SpringConstants.PUT_MAPPING,SpringConstants.DELETE_MAPPING,SpringConstants.PATCH_MAPPING), true);

        List<String> classUrls = PsiElementUtil.parsePsiAnnotationAttributeValue(classUrlPsiAnnotation, "value", "path");
        List<String> methodUrls = PsiElementUtil.parsePsiAnnotationAttributeValue(methodUrlPsiAnnotation,"value","path");

        return handleHttpUrl(classUrls, methodUrls);
    }


    private List<String> handleHttpUrl(List<String> classUrls, List<String> methodUrls){
        List<String> mappingUrls = new ArrayList<>();
        if (classUrls == null || classUrls.isEmpty()){
            if (methodUrls == null || methodUrls.isEmpty()){
                //classUrls 为空， methodUrls 为空
                mappingUrls.add("/");
            }else {
                //classUrls 为空， methodUrls 不为空
                for (String methodUrl : methodUrls) {
                    mappingUrls.add(handleUrl(methodUrl));
                }
            }
        }else {
            if (methodUrls == null || methodUrls.isEmpty()){
                //classUrls 不为空， methodUrls 为空
                for (String classUrl : classUrls) {
                    mappingUrls.add(handleUrl(classUrl));
                }
            }else {
                //classUrls 不为空， methodUrls 不为空
                for (String classUrl : classUrls) {
                    for (String methodUrl : methodUrls) {
                        if (classUrl == null || classUrl.length() == 0){
                            mappingUrls.add(handleUrl(methodUrl));
                        }else {
                            mappingUrls.add(handleUrl(classUrl) +  handleUrl(methodUrl));
                        }
                    }
                }
            }
        }

        return mappingUrls;
    }

    private String handleUrl(String url){
        if (!url.startsWith("/")){
            url = "/" + url;
        }

        if (url.endsWith("/")){
            url = url.substring(0, url.length() - 1);
        }

        return url;
    }

}
